#modeltag({tagcode:"jxbrms_systemrole",id:params[0]})
#include("/common/layout/bootstrap_form_layout.html")
#@layout()
#define head()
#define title()
#(jxrb.message(actionName,"systemRoleMenu")) #(systemrole.role_name ??)
#end
#jx_css({file:"zTreeStyle.css,table_tree.css"})
<script language="javascript">
  seajs.use(["jquery", "jx_msgbox_plus","jx_pinyin","jx_check"], function ($, msgbox,jxpy) {
    
    jxpy({nameObject: "#roleName", codeObject: "#roleCode"});
    
    $("#b_submit").on("click",function(){
      if (!jxCheck.check())
      return false;
      jz();
      msgbox.confirm({url: "#(controllerKey)#(action)/"},
              function ()
              {
                msgbox.close();
                location.href = "#(controllerKey)";
              }, function ()
      {
        if (msgbox.messageType != 2)
          location.href = "#(controllerKey)";
      });
      
    })
    

    $(function () {
      $("input[type='checkbox']").click(function () {
        var menuid = $(this).attr("id");
        var pid = $(this).attr("parentid");
        var isSel = $(this).is(":checked");
        selectChChkbox(menuid, isSel);
        if (isSel) {
          $("#" + menuid).attr("state", 1);
          $("#" + menuid).attr("select_num", $("#" + menuid).attr("chirden"))
          $("#" + menuid).siblings('label').attr("class", "");
        } else {
          $("#" + menuid).attr("state", 0);
          $("#" + menuid).attr("select_num", 0)
          $("#" + menuid).siblings('label').attr("class", "");
        }
        if (pid !== "") {
          selectPaChkbox(pid);
        }

      });
    });
    function selectChChkbox(menuid, isSel)
    {

      $("input[parentid='" + menuid + "']").each(function ()
      {
        if (isSel)
        {
          $(this).prop("checked", true);
          $(this).attr("state", 1);
          $(this).attr("select_num", $(this).attr("chirden"))
          $(this).siblings('label').attr("class", "");
        } else
        {
          $(this).prop("checked", "");
          $(this).attr("state", 0);
          $(this).attr("select_num", 0)
          $(this).siblings('label').attr("class", "");
        }
        if ($(this).attr("chirden") > 0) {
          selectChChkbox($(this).attr("id"), isSel)
        }
      });
    }

    function selectPaChkbox(pid) {
      console.log("父ID:" + pid);
      var sel = 0
      $("input[parentid='" + pid + "']").each(function () {
        if ($(this).is(":checked") && parseInt($(this).attr("state")) === 1)
        {
          sel += 2
        } else if ($(this).is(":checked") && parseInt($(this).attr("state")) === 2) {
          sel += 1
        }
      });

      $("#" + pid).attr("select_num", sel);
      if (getselNum(pid) === 0) {
        $("#" + pid).prop("checked", false);
        $("#" + pid).siblings('label').attr("class", "");
        $("#" + pid).attr("state", 0);
      } else if (getselNum(pid) < getchirNum(pid)) {
        $("#" + pid).siblings('label').attr("class", "half3");
        $("#" + pid).attr("state", 2);
        $("#" + pid).prop("checked", true);
      } else if (getselNum(pid) === getchirNum(pid)) {
        $("#" + pid).siblings('label').attr("class", "");
        $("#" + pid).attr("state", 1);
        $("#" + pid).prop("checked", true);
      }
      if ($("#" + pid).attr("parentid") !== "") {
        selectPaChkbox($("#" + pid).attr("parentid"));
      }
    }

    function getselNum(id) {
      var selNum = $("#" + id).attr("select_num")
      if (isNaN(selNum)) {
        selNum = 0
      }
      return parseInt(selNum)
    }

    function getchirNum(id) {
      var chirNum = $("#" + id).attr("chirden")
      return parseInt(chirNum) * 2
    }

    function jz() {
      $("#menu_id").val(getCheckedMenus("menu"));
      $("#module_id").val(getCheckedMenus("module"));
    }

    $(function () {
      $("input[state='2']").each(function () {
        set2($(this).attr("id"));
      });
      $("input[state='2']").each(function () {
        $(this).siblings('label').attr("class", "half3");
      });
    });
    function set2(pid) {
      $("#" + pid).attr("state", 2);
      if ($("#" + pid).attr("parentid") !== "") {
        set2($("#" + pid).attr("parentid"));
      }
    }

    $(function () {
      $("a").click(function () {
        xs($(this));
      });
    });
    function xs(obj) {
      if ($(obj).parents('.data').attr('state') === '0') {
        $(obj).children('.icon_plus').attr("class", "icon_sub");
        $(obj).children('.icon_file').attr("class", "icon_openfile");
        $(obj).parents('.data').attr("state", "1");
      } else {
        $(obj).children('.icon_sub').attr("class", "icon_plus");
        $(obj).children('.icon_openfile').attr("class", "icon_file");
        $("input[parentid='" + $(obj).attr("value") + "']").each(function () {
          if ($(this).parents('.data').attr("state") === '1') {
            $(this).parents('.divtd').children('a:eq(0)').children('.icon_sub').attr("class", "icon_plus");
            $(this).parents('.divtd').children('a:eq(0)').children('.icon_openfile').attr("class", "icon_file");
            $(this).parents('.data').attr("state", "0");
            changexh($(this).attr("id"))
          }
        });
        $(obj).parents('.data').attr("state", "0");
      }
      changexh($(obj).attr("value"));
    }

    function changexh(menuid) {
      $("input[parentid='" + menuid + "']").each(function () {
        if ($(this).parents('.data').css('display') === 'none') {
          $(this).parents('.data').css('display', '');
        } else {
          $(this).parents('.data').css('display', 'none');
        }
      });
    }

    function getCheckedMenus(menu) {
      menus = "";
      $("input[flag='"+menu+"']").each(function () {
        if ($(this).is(':checked')) {
          menus += $(this).attr("id");
          menus += ",";
        }
      });
      return menus;
    }
    $("#b_return").on("click", function (){
      location.href = "#(controllerKey)"
    })


    window.set2 = set2;
    window.getchirNum = getchirNum;
    window.selectPaChkbox = selectPaChkbox;
    window.getselNum = getselNum;
    window.selectChChkbox = selectChChkbox;
    window.xs = xs;
    window.getCheckedMenus = getCheckedMenus;
    window.jz = jz;
    window.changexh = changexh;
  });
</script>
#end

#define bodyExt()
class="gray-bg"
#end

#define form()
#include("info.html")
#end

#define formAfter()
<center>
  #jx_button({value:"action.save",class:"btn btn-primary"})&nbsp;&nbsp;&nbsp;&nbsp;
  #jx_button({value:"action.return",id:"b_return",class:"btn btn-danger"})
</center>
#end
